package com.chagee.store.operation.server.controller;

import com.alibaba.fastjson.JSON;
import com.chagee.base.utils.HttpResponse;
import com.chagee.base.utils.HttpResponseUtil;
import com.chagee.base.utils.PaginationData;
import com.chagee.base.utils.PaginationRequest;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.exception.BizException;
import com.chagee.store.operation.common.utils.CommonUtil;
import com.chagee.store.operation.server.facade.AppealReviewerFacade;
import com.chagee.store.operation.server.model.param.*;
import com.chagee.store.operation.server.model.vo.AppealReviewerRecordVO;
import com.chagee.store.operation.server.model.vo.EmployeeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhiqin.yuan
 * @since 2024-06-27
 */
@Slf4j
@RestController
@RequestMapping("/appealReviewer")
@Api(tags = "三方打烊审核申诉人配置")
public class AppealReviewerConfigurationController {

	@Resource
	private AppealReviewerFacade appealReviewerFacade;

	/**
	 * 新建申诉人配置
	 * @param param
	 * @return
	 */
	@PostMapping("/addAppealReviewer")
	@ApiOperation("添加审核人")
	public HttpResponse<Void> addAppealReviewer(@RequestBody @Valid AddAppealReviewerParam param) {
		log.info("addAppealReviewer_request:{}, auth:{}", JSON.toJSONString(param), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(param))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(param));
		}
		try {
			appealReviewerFacade.addAppealReviewer(param);
		}catch (BizException e){
			return  HttpResponseUtil.build(null,e.getErrorCode(),e.getReturnMsg());
		}
		return HttpResponseUtil.success(null);
	}

	/**
	 * 分页查询审核人信息
	 *
	 * @param request req
	 * @return resp
	 */
	@PostMapping("/pageQueryAppealReviewer")
	@ApiOperation("审核人数据分页查询")
	public HttpResponse<PaginationData<AppealReviewerRecordVO>> pageQueryAppealReviewer(@RequestBody PaginationRequest<AppealReviewerListParam> request) {
		log.info("pageQueryAppealReviewer_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (Objects.isNull(request.getPageNum()) || request.getPageNum() == 0) {
			request.setPageNum(1);
		}
		if (Objects.isNull(request.getPageSize()) || request.getPageSize() == 0) {
			request.setPageSize(10);
		}
		return HttpResponseUtil.success(appealReviewerFacade.pageQueryAppealReviewer(request));
	}


	/**
	 * 分页查询审核人信息
	 * @param param
	 * @return
	 */
	@PostMapping("/deleteAppealReviewer")
	@ApiOperation("审核人数据删除")
	public HttpResponse<Boolean> deleteAppealReviewer(@RequestBody DeleteReviewerParam param) {
		log.info("deleteAppealReviewer_request:{}, auth:{}", param, JSON.toJSONString(AuthUtil.getUser()));
		return HttpResponseUtil.success(appealReviewerFacade.deleteAppealReviewer(param.getReviewerId()));
	}

	/**
	 * 模糊查询人事人员信息
	 * @param param
	 * @return
	 */
	@PostMapping("/queryReviewer")
	@ApiOperation("模糊查询人员信息")
	public HttpResponse<PaginationData<EmployeeVo>> queryReviewer(@RequestBody QueryReviewerParam param) {
		log.info("queryReviewer_request:{}, auth:{}", param, JSON.toJSONString(AuthUtil.getUser()));
		if (Objects.isNull(param.getPageNum()) || Objects.equals(0, param.getPageNum())) {
			param.setPageNum(1);
		}
		if (Objects.isNull(param.getPageSize()) || Objects.equals(0, param.getPageSize())) {
			param.setPageSize(10);
		}
		return HttpResponseUtil.success(appealReviewerFacade.queryReviewer(param));
	}

	/**
	 * 修改申诉人配置
	 * @param param
	 * @return
	 */
	@PostMapping("/updateAppealReviewer")
	@ApiOperation("修改审核人信息")
	public HttpResponse<Boolean> updateAppealReviewer(@RequestBody @Valid UpdateAppealReviewerParam param) {
		log.info("updateAppealReviewer_request:{}, auth:{}", JSON.toJSONString(param), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(param))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(param));
		}
		appealReviewerFacade.updateAppealReviewer(param);

		return HttpResponseUtil.success(true);
	}

}
